﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using dpptask2.Exceptions;

namespace dpptask2.OptionClasses
{
    public class optionEnumAttribute : optionParametrizedAttribute
    {
        public string[] values = null;

        public override Type typeAccepted()
        {
            return typeof(string);
        }

        public override object createValueFromString(string stringValue)
        {
            if (!values.Contains(stringValue))
            {
                string optionList = "";
                foreach (string option in this.values)
                    optionList += string.Format("'{0}'{1}", option, ", ");

                if (optionList.EndsWith(", "))
                    optionList = optionList.Substring(0, optionList.Length - 2);

                throw new ParserException("'{0}' is not an option in enumerated values, only {1} allowed.", stringValue, optionList);
            }

            return stringValue;
        }

        private void valueCheck()
        {
            if (values == null || values.Count() == 0)
                throw new ParserException("Property {0} has no enum values specified.", fieldFullName());

            if (defaultValue != null)
            {
                if (!values.Contains(defaultValue))
                    throw new ParserException("Property {0} has defaultValue specified as '{1}', although this value is not contained in enumerated values.", fieldFullName(), defaultValue);
            }
        }

        public override void check()
        {
            base.check();
            valueCheck();
        }
    }
}
